﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;

namespace CMTVision.Widgets.Framework
{
    public class BaseWidgetContainer : System.Web.UI.UserControl, CMTVision.Widgets.Framework.IWidgetContainer
    {
        #region Ctor

        public BaseWidgetContainer()
            : base()
        {
            IsInEditMode = false;
            TitleShowMode = TitleShowMode.Normal;
        }

        #endregion

        #region Implement IWidgetContainer interface

        public bool IsInEditMode
        {
            get;
            set;
        }

        public TitleShowMode TitleShowMode
        {
            get;
            set;
        }

        public virtual System.Web.UI.Control WidgetHolder
        {
            get { throw new InvalidOperationException("Need to implement the property:'WidgetHolder'!"); }
        }

        public virtual System.Web.UI.Control ContainerWrapper
        {
            get { throw new InvalidOperationException("Need to implement the property:'ContainerWrapper'!"); }
        }



        public IWidget Widget
        {
            get;
            set;
        }

        public void LoadWidget(long widgetId)
        {
            if (CanLoadWidgets)
            {
                Model.Widget widgetInfo = null;
                using (Model.CMTWidgetsEntities entity = CMTVision.Common.EntitiesManager.LoadWidgetEntity())
                {
                    widgetInfo = (from w in entity.Widgets.Include("WidgetType")
                                  where w.ID == widgetId
                                      && w.WidgetType != null
                                  select w).FirstOrDefault();
                }

                if (widgetInfo != null)
                {
                    TitleShowMode = (Framework.TitleShowMode)widgetInfo.TitleShowMode;

                    var widgetControl = Page.LoadControl(widgetInfo.WidgetType.Src);
                    widgetControl.ID = Util.GenerateID<IWidget>(widgetInfo.ID.ToString());

                    IWidget _widget = widgetControl as IWidget;
                    if (_widget != null)
                    {
                        _widget.WidgetID = widgetId;
                        _widget.State = widgetInfo.State;
                        _widget.IsInEditting = false;
                        _widget.Initialize();
                        WidgetHolder.Controls.Add(widgetControl);

                        Widget = _widget;
                        _widget.InitContainer(this);
                        (ContainerWrapper as System.Web.UI.IAttributeAccessor).SetAttribute(Constants.Attribute_WidgetTypeID, widgetInfo.WidgetType.ID.ToString());
                        (ContainerWrapper as System.Web.UI.IAttributeAccessor).SetAttribute(Constants.Attribute_WidgetID, widgetId.ToString());
                        (ContainerWrapper as System.Web.UI.IAttributeAccessor).SetAttribute(Constants.Attribute_Index, widgetInfo.OrderNo.ToString());
                    }
                }
            }
        }


        public virtual void Refresh()
        {
            Widget.Refresh();
        }

        #endregion

        #region Other Methods     

        protected bool CanLoadWidgets
        {
            get
            {
                return WidgetHolder != null
                    && ContainerWrapper != null
                    && ContainerWrapper is System.Web.UI.IAttributeAccessor;
            }
        }

        #endregion


        public Action<IWidgetContainer, IWidget> Deleted { get; set; }


    }
}